<?php

namespace App\Http\Controllers\Api\V1\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use App\Models\ScenicDivination;
use App\Models\YaoDivination;
use Storage;

class UserDivinationRecordYzyItemResource extends JsonResource
{
    public function toArray($request)
    {
    	$resource = $this->resource;
    	
        $categoryId = request()->category_id;

        $bmcs = "";
        $bycs = "";
        $wxsk = "";
        $wxzy = "";
         if($categoryId){
            $res = $resource->divinationCategoriesRelations()->where('category_id',$categoryId)->first();
            if($res){

                $content =$res->content;
                $yaoContent = YaoDivination::find($res->yao_divination_id);
                $bmcs = ScenicDivination::query()->where("divination_id",$yaoContent->divination_id)->where("scenic_area_id",1)->first()->toArray();
                $bycs = ScenicDivination::query()->where("divination_id",$yaoContent->by_dividend_id)->where("scenic_area_id",1)->first()->toArray();

                if($bmcs){
                    $bmcs['image'] = Storage::disk('public')->url($bmcs['image']);
                }
                if($bycs){
                    $bycs['image'] = Storage::disk('public')->url($bycs['image']);
                }
               
                $wxsk = $yaoContent->wxsk;
                $wxzy = $yaoContent->wxzy;
                $by = config("site.bianyao")[$yaoContent->yao_count];
            }

        }
        $content = "";
        if($categoryId){
            $res = $resource->divinationCategoriesRelations()->where('category_id',$categoryId)->first();
            if($res){
                $content =$res->content;
            }

        }
        $divination = new divinationYzyItemResource($resource->divination);
        return [
        	'id' => (int)$resource->id,
	        'divination'=>$divination,
            'is_show'=>$resource->is_show,
            'cause'=>$resource->cause,
            'content'=>$content,
            'bmcs'=>$bmcs,
            'bycs'=>$bycs,
            'wxsk'=>$wxsk,
            'wxzy'=>$wxzy,
            'type'=>$resource->type,
	        'created_at' => (string)$resource->created_at,
        ];
    }
}
